﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing;
using System.Net.Sockets;

using libReadWrite;
using libCommonClass;

namespace TTServer.Messages
{
    public class SlideChangedMessage : TTMessage
    {
        private String _temporaryKey = "";

        public String TemporaryKey
        {
            get { return _temporaryKey; }
            set { _temporaryKey = value; }
        }

        private byte[] _data = null;

        public byte[] Data
        {
            get { return _data; }
            set { _data = value; }
        }

        public SlideChangedMessage(String temporaryKey, Image img)
        {
            this._temporaryKey = temporaryKey;

            using (var ms = new System.IO.MemoryStream())
            {
                //var enc = GetCodecInfo("image/jpeg");
                //var eps = new EncoderParameters(1);
                //eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 8L);

                //img.Save(ms, enc, eps);


                img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                _data = ms.ToArray();


                //System.Diagnostics.Debug.WriteLine("ImageSize = {0}", bytes.Length);
                System.Diagnostics.Debug.WriteLine(String.Format("ImageSize = {0}", _data.Length));
            }
        }

        public SlideChangedMessage()
        {
            // TODO: Complete member initialization
        }

        public override void ReadMessage(BigEndianReader sr)
        {
            throw new NotImplementedException();
        }

        public override void WriteMessage(BigEndianWriter sw)
        {
            sw.Write((byte)MessageKind.MESSAGE_SLIDE_CHANGED);
            sw.Write(Global.NO_LENGTH_CODE);

            sw.WriteString(this.TemporaryKey);

            sw.Write(_data.Length);
            sw.Write(_data);
        }

        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            foreach (ImageCodecInfo encoder in ImageCodecInfo.GetImageEncoders())
                if (encoder.MimeType == mimeType)
                    return encoder;

            throw new ArgumentOutOfRangeException(
                string.Format("'{0}' not supported", mimeType));
        }

        public override TTMessage Clone()
        {
            return new SlideChangedMessage();
        }

        public override int GetMessageID()
        {
            return MessageKind.SLIDE_CHANGED;
        }

        public override string GetMessageName()
        {
            return "SlideChanged";
        }
    }
}
